package com.yuchen.learning.controller;

import com.yuchen.learning.common.Result;
import com.yuchen.learning.entity.StudyRoute;
import com.yuchen.learning.service.StudyRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/route")
public class StudyRouteController {
    @Autowired
    private StudyRouteService studyRouteService;

    // 新增学习路线
    @PostMapping("/add")
    public Result<?> add(@RequestBody StudyRoute studyRoute) {
        boolean ok = studyRouteService.save(studyRoute);
        return ok ? Result.success(null) : Result.error("添加失败");
    }

    // 查询所有路线（可按用户过滤）
    @GetMapping("/list")
    public Result<List<StudyRoute>> list(@RequestParam(required = false) Long userId) {
        List<StudyRoute> routes = (userId == null)
                ? studyRouteService.list()
                : studyRouteService.lambdaQuery().eq(StudyRoute::getUserId, userId).list();
        return Result.success(routes);
    }

    // 更新路线
    @PutMapping("/update")
    public Result<?> update(@RequestBody StudyRoute studyRoute) {
        boolean ok = studyRouteService.updateById(studyRoute);
        return ok ? Result.success(null) : Result.error("更新失败");
    }

    // 删除路线
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        boolean ok = studyRouteService.removeById(id);
        return ok ? Result.success(null) : Result.error("删除失败");
    }

    // 查询路线详情
    @GetMapping("/detail/{id}")
    public Result<StudyRoute> detail(@PathVariable Long id) {
        StudyRoute route = studyRouteService.getById(id);
        return route != null ? Result.success(route) : Result.error("路线不存在");
    }
}